import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mirror/AppStyle.dart';
import 'package:mirror/page/home/pages/FindPage.dart';
import 'package:mirror/page/home/pages/MinePage.dart';
import 'package:mirror/page/home/pages/home/HomePage.dart';
import 'package:mirror/page/home/pages/message/MessagePage.dart';
import 'package:mirror/service/WebSocketService.dart';

class MainPage extends StatefulWidget {
  const MainPage({super.key});

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  List<Widget> pages = [
    const HomePage(),
    const FindPage(),
    const MessagePage(),
    const MinePage(),
  ];

  int _currentIndex = 0;

  @override
  void initState() {
    super.initState();
    WebSocketService.connect();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: IndexedStack(
          index: _currentIndex,
          children: pages,
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: const [
          BottomNavigationBarItem(
              icon: Icon(Icons.home_outlined),
              label: "首页",
              tooltip: "首页",
              activeIcon: Icon(Icons.home)),
          BottomNavigationBarItem(
              icon: Icon(Icons.find_in_page_outlined),
              label: "发现",
              tooltip: "发现",
              activeIcon: Icon(Icons.find_in_page)),
          BottomNavigationBarItem(
              icon: Icon(Icons.message_outlined),
              label: "消息",
              tooltip: "消息",
              activeIcon: Icon(Icons.message)),
          BottomNavigationBarItem(
              icon: Icon(Icons.person_outline),
              label: "我的",
              tooltip: "我的",
              activeIcon: Icon(Icons.person)),
        ],
        currentIndex: _currentIndex,
        onTap: (i) {
          if (i != 3) {
            SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
                statusBarColor: Constant.mainColor,
                statusBarIconBrightness: Brightness.light));
          } else {
            SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
                statusBarColor: Colors.transparent,
                statusBarIconBrightness: Brightness.dark));
          }
          setState(() {
            _currentIndex = i;
          });
        },
      ),
    );
  }
}
